function save(isClosed) {
	var oEditor = FCKeditorAPI.GetInstance('wf_editor');
	var ele = vilidateName(oEditor);
	if(ele){
		var callback = function(){
			SelectField(oEditor,ele);
			return true;
		};
		new parent.Milan.widget.Dialog('提示信息',"控件名称为空！",{confirmCallback:callback,closeCallBack:callback});
		return;
	}
	if(oEditor.EditorDocument.getElementById("red_dotted_absolute"))
		oEditor.EditorDocument.body.removeChild(oEditor.EditorDocument.getElementById("red_dotted_absolute"));
	var content = oEditor.GetXHTML(true);
	if(content.indexOf("<object")>-1||content.indexOf("<form")>-1||content.indexOf("<iframe")>-1||content.indexOf("<frameset")>-1||content.indexOf("<frame")>-1){
		new parent.Milan.widget.ConfirmDialog('提示信息',"表单中包含非法控件！必须删除，否则表单将无法使用！确定删除？",{
			confirmCallback : function(){
				content=content.replace(/\s+/g," ").replace(new RegExp("<object.+?</object>",'gi'),"");
				content = content.replace(/<form[^>]*>/g,"").replace(/<\/form>/g,"");
				content = content.replace(/<iframe[^>]*>/g,"").replace(/<\/iframe>/g,"");
				content = content.replace(/<frameset[^>]*>/g,"").replace(/<\/frameset>/g,"");
				content = content.replace(/<frame[^>]*>/g,"");
				oEditor.SetHTML(content);
				saveFunction(content,oEditor,isClosed);
			}
		});
	}else{
		saveFunction(content,oEditor,isClosed);
	}
}
function saveFunction(content,oEditor,isClosed){
	oEditor.changeContent=false;
	//清除img 的 src 中http://oa.35.cn
	var path = "(src=[\"' ])"+window.location.protocol+'//'+window.location.host;
	content = content.replace(new RegExp(path, "g"),"$1");
	content = content.replace(new RegExp("0.5pt", "g"),"1pt");
	oEditor.SetHTML(content);
	
	var param = 'wformid='+document.getElementById("workfId").value+'&strContent=' + encodeURIComponent(content);
	/*parent.Milan.ajaxutility.getResult('post',
			'workingFormAction.do', function() {
				new parent.Milan.widget.Dialog('提示信息',"保存成功！");
			}, param);*/
	parent.Milan.ajaxutility.formSubmit({
		url:'workform/saveFormDesign.do',
		params:param,
		success:function(response)
		{
			if(!isClosed)
				new parent.Milan.widget.Dialog('提示信息',"保存成功！");
		}
	});
}
String.prototype.Trim = function()
{
	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
}
function SelectField(oEditor, element )
{
	oEditor.Selection.SelectNode(element);
	if((element.type && (element.type=="radio" ||  element.type=="checkbox")) || element.getAttribute('macrotype') && element.getAttribute('macrotype')=="MacroDateTime"){
		var timeSpan=element.parentNode;//如果是日历控件或者是单选框或者是复选框，则选中整个控件
		if(timeSpan.tagName=='SPAN' ){
			oEditor.Selection.SelectNode(timeSpan);
		}
	}
	//element.focus() ;
	if (element.tagName=="SELECT" && element.select )
		element.select() ;
}
function vilidateName(oEditor){
	var oDom = oEditor.EditorDocument;
	var inputs =oDom.getElementsByTagName('INPUT');
	var textareas=oDom.getElementsByTagName('TEXTAREA');
	var selects=oDom.getElementsByTagName('SELECT');
	var name="";
	if(inputs&&inputs.length>0){
		for(var i=0,len=inputs.length;i<len;i++){
			if(inputs[i]){
				name=inputs[i].getAttribute('name')||inputs[i].name;
				if(!name||name.Trim()==""){
					return inputs[i];
				}
			}
		}
	}
	if(textareas&&textareas.length>0){
		for(var i=0,len=textareas.length;i<len;i++){
			if(textareas[i]){
				name=textareas[i].getAttribute('name')||textareas[i].name;
				if(!name||name.Trim()==""){
					return textareas[i];
				}
			}
		}
	}
	if(selects&&selects.length>0){
		for(var i=0,len=selects.length;i<len;i++){
			if(selects[i]){
				name=selects[i].getAttribute('name')||selects[i].name;
				if(!name||name.Trim()==""){
					return selects[i];
				}
			}
		}
	}
	return null;
};
function setTemplate(template,editor) {
	editor=editor||'wf_editor';
	var oEditor = FCKeditorAPI.GetInstance(editor);
	oEditor.InsertHtml(template);
}

function popCustomTemplate(instanceName) {
	var url = "workingFormAction.do?&actionType=18&instanceName="+instanceName;
	win = window.parent.Milan.commonutils.createModelPop( {
		title : "选择表单模板",
		height : "500px",
		width : "800px"
	});
	window.parent.Milan.ajaxutility.updateContentUseAjax("post", url, win);
}

function FCKeditor_OnComplete(editorInstance) {
	bindCustomEvent( "click", popCustomTemplate,editorInstance.Name);
}

function bindCustomEvent(event, handle,instanceName ) {
	var content=false;
	if (!FCKeditorAPI.customEvents) {
		FCKeditorAPI.customEvents = [];
	}
	for(var i=0,len=FCKeditorAPI.customEvents.length;i<len;i++){
    	if(FCKeditorAPI.customEvents[i].instanceName==instanceName){
    		content=true;
 		} 	
 	}
 	if(!content){
 		var esh = new eventsHandle(event, handle,instanceName );
 		FCKeditorAPI.customEvents.push(esh);
 	} 
}

function eventsHandle( event, handle,instanceName) {
	this.event = event;
	this.handle = handle;
	this.instanceName=instanceName;
}
function outsideButtEvent(type,editor,obj) {
	if(obj&&obj.getAttribute("disabled")=="disabled")return;
	if(editor == "freeflow_formContent"){
		editor = Ext.milan.pagejs.page_archive.freeFlowFCKeditor;
	}
	var oEditor = FCKeditorAPI.GetInstance(editor);
	if((Milan.navigator.isIE && Milan.navigator.version == 6.0)||!!window.ActiveXObject&&!!document.documentMode){
		//此代码为了解决ie8中插入控件，不能定位的问题
		var selection = oEditor.Selection ;
		var oRange ;
		try{
			oRange = selection.GetBoundaryParentElement() ;
		}catch (e){
			oRange= null ;
		}
	}
	oEditor.Commands.GetCommand(type).Execute();
	if(obj)obj.blur();   
};
